//
//  KNNetworkingManager.h
//  Backhome
//
//  Created by kwep_vbn on 2017/3/22.
//  Copyright © 2017年 SmartHome. All rights reserved.
//

#import <Foundation/Foundation.h>

typedef enum : NSUInteger {
    KNRequestSerializerJSON = 1,
    KNRequestSerializerPlainText,
} KNRequestSerializer;

typedef enum : NSUInteger {
    KNResponseSerializerJSON = 1,
    KNResponseSerializerXML,
    KNResponseSerializerData
} KNResponseSerializer;


/**
 网络请求回调成功
 */
typedef void(^KNNetworkingSuccess)(id result,NSURLResponse *response);


/**
 网络请求失败
 */
typedef void(^KNNetworkingError)(NSError *errorInfo,NSURLResponse *response);


@interface KNNetworkingManager : NSObject

@property (strong, nonatomic) NSMutableArray <NSURLSessionDataTask *> *allTasks;

+ (instancetype)shareInstance;

/**
 设置baseUrl

 */
+ (void)setBaseUrl:(NSString *)baseUrl;


/**
 获取baseUrl

 */
+ (NSString *)baseUrl;


/**
 设置请求类型
 */
+ (void)setRequestSerializer:(KNRequestSerializer)requestSerializer;

+ (KNRequestSerializer)requestSerializer;

/**
 设置返回类型
 */
+ (void)setResponseSerializer:(KNResponseSerializer)responseSerializer;

+ (KNResponseSerializer)responseSerializer;

/**
 设置通用请求头

 */
+ (void)setCommonHeaders:(NSMutableDictionary *)headers;

+ (NSMutableDictionary *)commonHeaders;

+ (void)setTimeout:(NSInteger)timeout;

+ (NSInteger)timeOut;


/**
 取消任务
 */
+ (void)cancelTask:(NSURLSessionDataTask *)task;

+ (void)cacelAllTask;


+ (NSURLSessionDataTask *)GET:(NSString *)URLString
                   parameters:(nullable id)parameters
                     progress:(nullable void (^)(NSProgress *downloadProgress))downloadProgress
                    success:(KNNetworkingSuccess)success
                    failure:(KNNetworkingError)failure;

+ (NSURLSessionDataTask *)POST:(NSString *)URLString
                    parameters:(nullable id)parameters
                      progress:(nullable void (^)(NSProgress *downloadProgress))downloadProgress
                       success:(KNNetworkingSuccess)success
                       failure:(KNNetworkingError)failure;

+ (NSURLSessionDataTask *)UPLOAD:(NSString *)URLString
                      parameters:(nullable id)parameters
                      uploadFile:(NSData *)data
                  uploadFileName:(NSString *)fileName
                        mimeType:(NSString *)mimeType
                        progress:(nullable void (^)(NSProgress *downloadProgress))downloadProgress
                         success:(KNNetworkingSuccess)success
                         failure:(KNNetworkingError)failure;

+ (NSURLSessionTask *)downloadFile:(NSString *)urlString
                          progress:(nullable void (^)(NSProgress *downloadProgress))progress
                       destination:(nullable NSURL*(^)(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response))destination
                           success:(KNNetworkingSuccess)success
                           failure:(KNNetworkingError)failure;


@end
